﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Compiler.LabForms
{
    public partial class BranchEdit : Form
    {
        private TreeView _treeBuilder;
        private TreeNode _node;

        public BranchEdit(TreeView treeBuilder)
        {
            _treeBuilder = treeBuilder;
            InitializeComponent();
            Text = @"Добавление ветви";
            typeBox.SelectedIndex = 0;
        }

        public BranchEdit(TreeView treeBuilder, TreeNode node)
        {
            _node = node;
            _treeBuilder = treeBuilder;
            InitializeComponent();
            Text = @"Редактирование ветви";
            int index;
            string value = "";
            string type = _node.Text.Split(':')[0];
            if (_node.Text.Split(':').Length > 1)
            {   
                value = _node.Text.Split(':')[1];
            }
            switch (type)
            {
                case "Procedure":
                    index = 0;
                    break;
                case "Const":
                    index = 1;
                    break;
                case "Ident":
                    index = 2;
                    break;
                case "Expression":
                    index = 3;
                    break;
                case "Call":
                    index = 4;
                    break;
                case "IfElseBlock":
                    index = 5;
                    break;
                case "If":
                    index = 6;
                    break;
                case "Else":
                    index = 7;
                    break;
                case "While":
                    index = 8;
                    break;
                case "Assign":
                    index = 9;
                    break;
                case "Block":
                    index = 10;
                    break;
                case "Minus":
                    index = 11;
                    break;
                default:
                    index = -1;
                    break;
            }
            valueBox.Text = value.Trim();
            if (index > 4)
            {
                valueBox.Enabled = false;
            }
            else
            {
                typeBox.SelectedIndex = index;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (valueBox.Enabled) {
                if (string.IsNullOrEmpty(valueBox.Text)) return;
            }
            if (_node != null)
            {
                _node.Text = typeBox.SelectedItem + @": " + valueBox.Text;
            }
            else
            {
                var newSubnode = new TreeNode(typeBox.SelectedItem + ": " + valueBox.Text);
                _treeBuilder.SelectedNode.Nodes.Add(newSubnode);
                newSubnode.Parent.Expand();
            }
            _treeBuilder.SelectedNode = null;
            Close();
        }

        private void typeBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (typeBox.SelectedIndex > 4)
            {
                valueBox.Enabled = false;
                valueBox.Clear();
            }
            else
            {
                valueBox.Enabled = true;
            }
        }

        private void valueBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ':')
            {
                e.Handled = true;
            }
        }
    }
}
